home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / COPYMOVE.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  16KB  |  575 lines

  1. #include "pt.h"
  2. #include "string.h"
  3.  
  4. void pascal
  5. /* XTAG:updateFile */
  6. updateFile(fileId, cp, len, tryShortcut)
  7.     int fileId, tryShortcut;
  8.     long cp, len;
  9. {
  10.     extern unsigned char msgBuffer[];
  11.     extern struct window *windowList;
  12.     extern struct window *selWindow;
  13.     extern long selBegin, selEnd;
  14.     extern unsigned char *screenMap;
  15.     extern int debug;
  16.     extern int scrRows, scrCols;
  17.  
  18.     register struct window *w;
  19.     struct window *lastW;
  20.     int i, n, minRow, maxRow, row2, col1, col2;
  21.     long cp1;
  22.  
  23.     lastW = NULL;
  24.     w = windowList;
  25.     while( w != NULL ) {
  26.         if( w->fileId == fileId )
  27.             lastW = w;
  28.         w = w->nextWindow;
  29.     }
  30.  
  31.     /* If the top window is the only window showing this file */
  32.     /* and no line feed was deleted and the line is in the window */
  33.     /* then try to do it faster */
  34.     if( tryShortcut && lastW == windowList
  35.                 && cp < windowList->posBotline ) {
  36.         posToxy(windowList, selBegin, &row2, &col2);
  37.         col1 = windowList->col1 + 1;
  38.         col2 = windowList->col2 - 1;
  39.         setMap(row2, col1, row2, col2, 1, 0x07);
  40.         n = -1;
  41.         cp1 = prevLine(windowList->fileId, selBegin, &n);
  42.         fillLine(windowList, cp1, row2, col1, col2);
  43.         updateScreen(row2, row2);
  44.         return;
  45.     }
  46.     /* redraw the affected windows */
  47.     setMap(0, 0, scrRows-1, scrCols-1, 1, 0x07);
  48.     w = windowList;
  49.     minRow = scrRows;
  50.     maxRow = -1;
  51.     while( 1 ) {
  52.         if( w->fileId != fileId ) {
  53.             /* just mask off the window area so later */
  54.             /* windows (underneath it) will not repaint it */
  55.             col1 = w->col1;
  56.             row2 = w->row2;
  57.             n = w->col2 - col1 + 1;
  58.             for(i = w->row1; i <= row2; i++)
  59.                 memset(screenMap+scrCols*i+col1, 0, n);
  60.         } else {
  61.             /* repaint the window as necessary */
  62.             if( cp <= w->posBotline )
  63.                 w->posBotline += len;
  64.             if( cp < w->posTopline ) {
  65.                 cp1 = w->posTopline + len;
  66.                 /* remember: len < 0 for a delete */
  67.                 /* a delete overlapping this window? */
  68.                 if( cp1 < cp ) {
  69.                     n = -1;
  70.                     cp1 = prevLine(w->fileId, cp, &n);
  71.                 }
  72.                 /* recalculate the line number by letting */
  73.                 /* prevLine count as far back as it can */
  74.                 n = 30000;
  75.                 prevLine(w->fileId, cp1, &n);
  76.                 w->numTopline = n + 1;
  77.                 w->posTopline = cp1;
  78.             }
  79.             drawWindow(w);
  80.             if( w->row1 < minRow )
  81.                 minRow = w->row1;
  82.             if( w->row2 > maxRow )
  83.                 maxRow = w->row2;
  84.         }
  85.         if( w == lastW )
  86.             break;
  87.         w = w->nextWindow;
  88.     }
  89.     if( maxRow != -1 )
  90.         updateScreen(minRow, maxRow);
  91. }
  92.  
  93. void pascal
  94. /* XTAG:updateTops */
  95. updateTops(fileId, cp, len)
  96.     int fileId;
  97.     long cp, len;
  98. {
  99.     extern unsigned char msgBuffer[];
  100.     extern struct window *windowList;
  101.     extern int debug;
  102.  
  103.     register struct window *w;
  104.     struct window *lastW;
  105.     int n;
  106.     long cp1;
  107.  
  108.     lastW = NULL;
  109.     w = windowList;
  110.     while( w != NULL ) {
  111.         if( w->fileId == fileId )
  112.             lastW = w;
  113.         w = w->nextWindow;
  114.     }
  115.     /* adjust the data of the affected windows */
  116.     w = windowList;
  117.     while( 1 ) {
  118.         if( w->fileId == fileId ) {
  119.             if( cp >= w->posBotline )
  120.                 goto nextWindow;
  121.             else
  122.                 w->posBotline += len;
  123.             if( cp < w->posTopline ) {
  124.                 cp1 = w->posTopline + len;
  125.                 /* a delete overlapping this window? */
  126.                 if( cp1 < cp ) {
  127.                     n = -1;
  128.                     cp1 = prevLine(w->fileId, cp, &n);
  129.                 }
  130.                 /* recalculate the line number by letting */
  131.                 /* prevLine count as far back as it can */
  132.                 n = 30000;
  133.                 prevLine(w->fileId, cp1, &n);
  134.                 w->numTopline = n + 1;
  135.                 w->posTopline = cp1;
  136.             }
  137.         }
  138.     nextWindow:
  139.         if( w == lastW )
  140.             break;
  141.         w = w->nextWindow;
  142.     }
  143. }
  144.  
  145. void pascal
  146. /* XTAG:exchWithScrap */
  147. exchWithScrap()
  148. {
  149.     extern unsigned char msgBuffer[];
  150.     extern struct changeItem scrapBuffer;
  151.     extern struct changeItem *change;
  152.     extern int nextChange;
  153.     extern long selBegin, selEnd;
  154.     extern struct window *selWindow;
  155.     extern struct openFile *files;
  156.     extern unsigned char *userMessages[];
  157.  
  158.     long length;
  159.     register struct changeItem *thisChange;
  160.     struct piece *firstPiece;
  161.  
  162.     /* check if this is a readOnly file */
  163.     if( files[selWindow->fileId].readOnly ) {
  164.         sprintf(msgBuffer, userMessages[READONLYFILE],
  165.             files[selWindow->fileId].origName);
  166.         msg(msgBuffer, 1);
  167.         return;
  168.     }
  169.  
  170.     /* remember what was in the scrap */
  171.     firstPiece = scrapBuffer.firstPiece;
  172.     length = scrapBuffer.length;
  173.     /* prevent this from getting freed when the scrap buffer is reused */
  174.     scrapBuffer.firstPiece = NULL;
  175.     
  176.     /* move the selection into the scrap */
  177.     deleteChars(selWindow->fileId, NOUPDATE, 1);
  178.  
  179.     /* record in the change history */
  180.     IncrementNextChange();
  181.     thisChange = &change[nextChange];
  182.     thisChange->type = CINSERT;
  183.     thisChange->position = selBegin;
  184.     thisChange->length = length;
  185.     thisChange->fileId = selWindow->fileId;
  186.     thisChange->firstPiece = firstPiece;
  187.  
  188.     /* copy the old scrap into the file */
  189.     copyPieces(firstPiece, selWindow, selBegin, length, 1);
  190.     
  191.     /* free the scrap pieces */
  192.     freePieces(firstPiece);
  193. }
  194.  
  195. void pascal
  196. /* XTAG:copyToScrap */
  197. copyToScrap(fromWindow, fromBegin, fromEnd)
  198.     struct window *fromWindow;
  199.     long fromBegin, fromEnd;
  200. {
  201.     extern unsigned char msgBuffer[];
  202.     extern struct changeItem scrapBuffer;
  203.     extern long addPosition;
  204.     extern int selMode;
  205.     extern int scrapMode;
  206.  
  207.     int fromId;
  208.     long fb, copyLength;
  209.     register struct piece *tempPP;
  210.  
  211.     fromId = fromWindow->fileId;
  212.     
  213.     /* eliminate the EOF marker from the selection */
  214.     fb = fileSize(fromId);
  215.     if( fromEnd >= fb ) {
  216.         if( fromBegin < fb )
  217.             fromEnd = fb-1;
  218.         else    /* only the EOF symbol is selected */
  219.             return;
  220.     }
  221.     copyLength = fromEnd - fromBegin + 1;
  222.  
  223.     /* free the old scrap buffer pieces */
  224.     freePieces(scrapBuffer.firstPiece);
  225.  
  226.     /* record in the scrap buffer */
  227.     scrapBuffer.type = 1;
  228.     scrapBuffer.length = copyLength;
  229.     tempPP = getFreePiece();
  230.     scrapBuffer.firstPiece = tempPP;
  231.     scrapMode = selMode;
  232.  
  233.     tempPP->file = ADDFILE;
  234.     tempPP->position = addPosition;
  235.     fb = fromBegin;
  236.     while( fb <= fromEnd ) {
  237.         writeChar( readChar(fromId, fb++), addPosition++);
  238.     }
  239.     tempPP->length = copyLength;
  240. }
  241.  
  242. void pascal
  243. /* XTAG:insScrap */
  244. insScrap(doInsert)
  245.     int doInsert;
  246. {
  247.     extern unsigned char msgBuffer[];
  248.     extern struct window *selWindow;
  249.     extern long selBegin, selEnd;
  250.     extern struct changeItem scrapBuffer;
  251.     extern long addPosition;
  252.     extern struct openFile *files;
  253.     extern struct changeItem *change;
  254.     extern int nextChange;
  255.      extern int selMode;
  256.     extern int scrapMode;
  257.     extern int maxFiles;
  258.     extern unsigned char *userMessages[];
  259.  
  260.     long limit, logByte;
  261.     struct piece *tempPP, *oldScrap;
  262.     register struct openFile *ff;
  263.     register struct changeItem *thisChange;
  264.  
  265.     /* check if this is a readOnly file */
  266.     if( files[selWindow->fileId].readOnly ) {
  267.         sprintf(msgBuffer, userMessages[READONLYFILE],
  268.             files[selWindow->fileId].origName);
  269.         msg(msgBuffer, 1);
  270.         return;
  271.     }
  272.  
  273.     /* See if the text in the scrap buffer is in an edit file rather */
  274.     /* then in the add file.  If it is we need to copy it to the add */
  275.     /* file. */
  276.     if( !scrapBuffer.type ) {
  277.         /* keep a pointer to the old piece list */
  278.         oldScrap = scrapBuffer.firstPiece;
  279.         /* scrapBuffer.length will not change */
  280.         
  281.         /* get a new piece and initialize the fields */
  282.         tempPP = getFreePiece();
  283.         tempPP->file = ADDFILE;
  284.         tempPP->position = addPosition;
  285.         tempPP->length = scrapBuffer.length;
  286.         
  287.         /* this will be the new scrapBuffer piece list */
  288.         scrapBuffer.type = 1;  /* now it is addFile only type */
  289.         scrapBuffer.firstPiece = tempPP;
  290.         
  291.         /* Now copy the characters into the add file */
  292.         logByte = 0;
  293.         limit = scrapBuffer.length;
  294.         /* simulate a file this was deleted from */
  295.         ff = &files[maxFiles];
  296.         ff->hiLogBuffer = -1;
  297.         ff->origHandle = scrapBuffer.fileId;
  298.         ff->fileSize = limit;
  299.         ff->logPiece = oldScrap;
  300.         ff->loLogPiece = 0;
  301.         ff->hiLogPiece = oldScrap->length - 1;
  302.         while( logByte < limit ) {
  303.             /* copy the characters in pp to the add file */
  304.             writeChar( readChar(maxFiles, logByte++),
  305.                 addPosition++);
  306.         }
  307.         
  308.         freePieces(oldScrap);  /* free the old piece chain */
  309.     }
  310.     if( doInsert ) {
  311.         /* record in the change history */
  312.         IncrementNextChange();
  313.         thisChange = &change[nextChange];
  314.         thisChange->type = CCOPY;
  315.         selMode = scrapMode;
  316.         selBegi